package com.example.orderminiappapi.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.example.orderminiappapi.bean.Response;
import com.example.orderminiappapi.bean.Search;
import com.example.orderminiappapi.bean.User;
import com.example.orderminiappapi.service.SearchService;
import com.example.orderminiappapi.service.ShopService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 获取搜寻页的推荐餐厅排行榜
     */
    @ResponseBody
    @RequestMapping(value = "/getHistorySearchList", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response getHistorySearchList(HttpServletRequest request) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String openId = verify.getClaim("openId").asString();

        User user = new User();
        user.setUserId(openId);

        List<Search> searches = searchService.getHistorySearchList(user);

        Response response = new Response("10000", "成功", searches);
        return response;
    }
}
